//
//  YuXiViewController.m
//  DialectShare
//
//  Created by timmy zhong on 3/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "YuXiViewController.h"
#import "YuXi.h"
#import "DialectProxy.h"
#import "YuXiDB.h"
#import "YuXiDialectsViewController.h"
#import "SearchViewController.h"

@interface YuXiViewController ()
@property (nonatomic, retain) NSArray *yuxis;
@property (nonatomic, retain) DialectProxy *proxy;
@property (nonatomic, retain) YuXiDB *yuxiDB;
- (void)search;
@end

@implementation YuXiViewController
@synthesize yuxis = _yuxis, proxy = _proxy, yuxiDB = _yuxiDB, tableView = _tableView;

- (void)dealloc
{
    self.yuxis = nil;
    self.proxy.delegate = nil;
    self.proxy = nil;
    self.yuxiDB = nil;
    self.tableView = nil;
    
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _yuxiDB = [[YuXiDB alloc] init];
    
    _proxy = [[DialectProxy alloc] initWithDelegate:self];
    [_proxy yuxis];
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"我要说" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(search)] autorelease];

    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - tableview delegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.yuxis count];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    
    YuXi *yuxi = [self.yuxis objectAtIndex:indexPath.row];
    cell.textLabel.text = yuxi.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuXiDialectsViewController *controller = [[YuXiDialectsViewController alloc] init];
    YuXi *yuxi = [self.yuxis objectAtIndex:indexPath.row];
    controller.yuxi = yuxi;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

- (void)search
{
    SearchViewController *controller = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

#pragma mark - ProxyDelegate
- (void)requestFailWithProxy:(Proxy *)proxy
{
    self.yuxis = [_yuxiDB yuxis];
    [self.tableView reloadData];
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    NSArray *yuxis = [_yuxiDB yuxis];
    self.yuxis = yuxis;
    [self.tableView reloadData];
}









@end
